# -*- coding: utf-8 -*-
# @Time    : 2021/3/22 21:01
# @Author  : Suwenyang
# @Email   : 2242142766@qq.com
# @File    : homework.py


# import subprocess
# from IPy import IP
#
# with open("ipup.txt", "w+", encoding="utf-8") as ipf:
#     ip = IP('192.168.0.0/24')
#     for x in ip:                                  # 输出网段的IP清单
#         print(x)
#
#         if subprocess.Popen("ping x", shell=True):
#             x = f"{str(x)}\n"
#             ipf.write(x)


# import subprocess
# for i in range(1,256):
#     result = subprocess.check_call(f"ping 192.168.0.{i}",shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE,encoding='utf-8',timeout=1)
#     print(result.returncode)
#     try:
#         if result.returncode == 0:
#             print(f"192.168.0.{i}可以ping通！")
#         else:
#             raise Exception
#     except IndexError:
#         print(f"192.168.0.{i} ping不通！")
#     except IndentationError:
#         print(f"192.168.0.{i}ping不通！")
#     except Exception:
#         print(f"192.168.0.{i}ping不通！")

import subprocess

def runCmd(cmd):
    res = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    sout, serr = res.communicate()
    return res.returncode, sout, serr, res.pid


for i in range(1,256):
    i = str(i)
    result = runCmd("ping 192.168.174"+i)
    if result[0] == 0:
        print(i,"能ping通")
        print(result[1])
    else:
        print(i,"ping不通")
        print(result[1])
        print(result[2])


















